//
//  APLoginManager.m
//  AqualPolo
//
//  Created by dong on 2024/5/2.
//

#import "APLoginManager.h"


@interface APLoginManager ()
@property (nonatomic, strong) APUserModel *userModel;
@end

@implementation APLoginManager

+ (APLoginManager *)shareInstance{
    static APLoginManager *shareId = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareId = [[self alloc] init];
    });
    return shareId;
}


- (void)loginWithToken:(NSString *)token oauthType:(NSInteger)oauthType{
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setNullObject:token forKey:@"token"];
    [parameters setNullObject:@(oauthType) forKey:@"oauthType"];
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    [parameters setNullObject:app_Version forKey:@"ver"];
    [APDataManager postWithUrlString:Login_URL parameters:parameters result:^(NSDictionary * _Nullable res) {
        if ([res[@"success"] integerValue] == 1) {
            [self loginSuccess:res];
        }else{
            [self loginFail:[res[@"msg"] description]];
        }
    } failBlock:^(NSString * _Nullable errorMsg) {
        [self loginFail:errorMsg];
    }];
}
- (void)loginFail:(NSString *)msg {
    [[NSNotificationCenter defaultCenter] postNotificationName:LoginFailNotification object:msg];
}
- (void)loginSuccess:(NSDictionary *)res{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:@"YES" forKey:@"isLogin"];
    NSString *token = [res[@"data"][@"token"] description];
    [userdefault setObject:token forKey:@"token"];
    NSDictionary *userInfo = res[@"data"][@"userInfo"];
    self.userModel = [APUserModel yy_modelWithDictionary:userInfo];
    [userdefault setObject:userInfo forKey:@"userInfo"];
    [[NSNotificationCenter defaultCenter] postNotificationName:LoginSuccessNotification object:nil];
}

- (void)updateUserInfo:(NSDictionary *)userInfo{
    self.userModel = [APUserModel yy_modelWithDictionary:userInfo];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:userInfo forKey:@"userInfo"];
}

- (void)updateAvatarInfo:(APImageModel *)model{
    self.userModel.avatar = model.mediaPath;
    self.userModel.avatarUrl = model.mediaUrl;
    self.userModel.avatarMiddleThumbUrl = model.middleThumbUrl;
    self.userModel.avatarThumbUrl = model.thumbUrl;
    NSDictionary *userInfo = [self.userModel yy_modelToJSONObject];
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:userInfo forKey:@"userInfo"];
  }

-(void)updateUserInfoByInternetSevice:(APLoginUserInfoUpdateResultBlock)callBack{
    APUserModel *userModel = [[APLoginManager shareInstance] userInfo];
    NSString *urlString = [NSString stringWithFormat:@"%@?userId=%@",getUserInfo,userModel.userId];
    [APDataManager getDataWithUrlString:urlString result:^(NSDictionary * _Nullable res) {
        [self updateUserInfo:res[@"data"]];
        callBack(true,@"");
    } failBlock:^(NSString * _Nullable errorMsg) {
        callBack(false,errorMsg);
    }];
 }

- (APUserModel *)userInfo{
    if (self.userModel) {
        return self.userModel;
    }
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSDictionary *userInfo = [userdefault objectForKey:@"userInfo"];
    self.userModel = [APUserModel yy_modelWithDictionary:userInfo];
    return self.userModel;
}

- (NSString *)token{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *token = [userdefault objectForKey:@"token"];
    return token;
}

- (BOOL)isLogin{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *res = [userdefault objectForKey:@"isLogin"];
    return [res isEqualToString:@"YES"];
}

- (void)logout{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault removeObjectForKey:@"userInfo"];
    [userdefault removeObjectForKey:@"isLogin"];
    self.userModel = nil;
}

@end
